import { Events } from 'ionic-angular';

import { BasePage } from './../../pages/basePage';
import { UserService } from './../../providers/UserService';
import { Component, Injector } from '@angular/core';
import { PIC_URL } from "./../../providers/Config";
import { StorageService } from '../../providers/StorageService';
declare var $: any;
/**
 * Generated class for the ShopYesServicesComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'shop-yes-services',
  templateUrl: 'shop-yes-services.html'
})
export class ShopYesServicesComponent  extends BasePage{
  
  shopList = [];  
  picrooturl ='';  //图片地址
  userinfo={}
  orderList3=[]     //获取的已服务
  flag:boolean;    //显示控制器
  pop=1;   // 页码
  sum:number;   //总单数
  bottom1:boolean=false;    //加载全部底部显示器
  
  constructor(injector: Injector, public userService: UserService, public events: Events,public stc:StorageService) {
    super(injector);
    this.picrooturl = PIC_URL;
    this.userinfo = this.stc.read('userinfo');
    this.getlist3('');
    events.subscribe('user:created', (hello, time) => {
      if(hello == 0){
        //待服务有数据更新
        console.log("全部有数据更新")
        this.again();
      }
    });
    // this.events.subscribe('shop:created', (status, id) => {
    //   console.log(id);
    //   this.shopList = [];
    //   if (id == 3) {
    //     this.shopList = status;
    //     if(this.shopList.length==0){
    //       this.noInfo=true;
    //     }else{
    //       this.noInfo=false;
    //     }
    //     console.log(this.shopList);
    //     return;
    //   }
    // });
  }
  async getlist3(infiniteScroll){
    let param={shop_id:this.userinfo['shop_id'],type_id:3,pop:this.pop}
    let result=await this.userService.shopOrderAPI(param);
    console.log(result);
    if(result.code == 1){
      if(this.pop==1){
        this.sum=result.count;
      }
      this.pop++;
      this.orderList3=this.orderList3.concat(result.data);
      if(this.orderList3.length ==0){
        this.flag=false;
      }else{
        this.flag=true;
      } 
      if(infiniteScroll){
        infiniteScroll.complete(); //请求完成数据更新页面
        if(result.data.length<6){
          $(".box2 ion-infinite-scroll-content").css("display","none");
          this.bottom1=true;
        }
      }
    }else{
      this.flag =false;
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    };
  }
  //无限滚动
  doInfinite(infiniteScroll) {
    this.bottom1 = false;
    this.getlist3(infiniteScroll);
  }
  again(){
    this.pop=1;
    this.bottom1 = false;
    this.orderList3=[];
    $(".box2 ion-infinite-scroll-content").css("display","block");
    this.getlist3('');
  }
}
